package net.lcheng.sellerCenter.controller;

import net.lcheng.model.BaseAreas;
import net.lcheng.model.User;
import net.lcheng.model.UserAccountDetails;
import net.lcheng.model.UserWithdrawalsRecord;
import net.lcheng.service.BaseAreasService;
import net.lcheng.service.UserAccountDetailsService;
import net.lcheng.service.UserService;
import net.lcheng.service.UserWithdrawalsRecordService;
import net.lcheng.vo.UserWithdrawalsRecordModel;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * Created by zwr on 2016/7/8 0008.
 */
@Controller
public class UserWithdrawalsRecordController {
    @Autowired
    private BaseAreasService baseAreasService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserWithdrawalsRecordService userWithdrawalsRecordService;
    @Autowired
    private UserAccountDetailsService userAccountDetailsService;
    /***
     * 提现金额Get
     */
    @RequestMapping(value = "user/withdraw", method = RequestMethod.GET)
    public String userWithdrawalsGet(Model model) {
        Subject currentUser= SecurityUtils.getSubject();
        if(currentUser.isAuthenticated()) {
            String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
            //获取用户信息
            User user = userService.getUserByPhone(userPhoneNum);
            model.addAttribute("user",user);
        }
        model.addAttribute("UserWithdrawalsRecordModel", new UserWithdrawalsRecordModel());
        return "user_withdraw";
    }
    /**
     * 提现金额Post
     */
    @RequestMapping(value = "user/withdraw", method = RequestMethod.POST)
    public String userWithdrawalsPost(Model model, @Valid @ModelAttribute("UserWithdrawalsRecordModel") UserWithdrawalsRecordModel userWithdrawalsRecordModel, BindingResult result) {
        Subject currentUser= SecurityUtils.getSubject();
        String userPhoneNum = currentUser.getSession().getAttribute("UserPhoneNum").toString();
        if (result.hasErrors()) {
            User user = userService.getUserByPhone(userPhoneNum);
            model.addAttribute("user",user);
            return "user_withdraw";
        }
        /**获取省名称*/
        BaseAreas provinceCode=baseAreasService.getAreaCodeByAreaName(userWithdrawalsRecordModel.getAddrProvinceCode());
        /**获取市名称*/
        BaseAreas cityCode=baseAreasService.getAreaCodeByAreaName(userWithdrawalsRecordModel.getAddrCityCode());
        User user = userService.getUserByPhone(userPhoneNum);
        UserAccountDetails userAccountDetails=new UserAccountDetails();
        String cashWithdrawalAmountString = userWithdrawalsRecordModel.getCashWithdrawalAmount();
        /**String类型转换为BigDecimal类型*/
        BigDecimal cashWithdrawalAmountBigDecimal = new BigDecimal(cashWithdrawalAmountString);
        //用户输入的提现金额和user里面的总金额对比
        int i=cashWithdrawalAmountBigDecimal.compareTo(user.getAccountBalance());
        //BigDecimal判断-1表示小于,0是等于,1是大于.判断他提现金额不能大于总金额
        if (i==1){
            model.addAttribute("ere","你的提现金额大于总金额");
            User userTokenBegin = userService.getUserByPhone(userPhoneNum);
            model.addAttribute("user",userTokenBegin);
            return "user_withdraw";
        }else if (i==0 || i==-1){
                //提现的金额和user金额相减
                double userAccountBalanceDouble= user.getAccountBalance().subtract(cashWithdrawalAmountBigDecimal).doubleValue();
                BigDecimal balance = new BigDecimal(Double.toString(userAccountBalanceDouble));
                /**提现记录*/
               UserWithdrawalsRecord userWithdrawalsRecord=new UserWithdrawalsRecord();
                userWithdrawalsRecord.setId(UUID.randomUUID().toString());
                userWithdrawalsRecord.setUserId(user.getUserId());
                userWithdrawalsRecord.setUserName(user.getUserName());
                userWithdrawalsRecord.setUserName(userWithdrawalsRecordModel.getAccountName());
                userWithdrawalsRecord.setCashWithdrawalAmount(cashWithdrawalAmountBigDecimal);
                userWithdrawalsRecord.setAccountName(userWithdrawalsRecordModel.getAccountName());
                userWithdrawalsRecord.setBranch(userWithdrawalsRecordModel.getBranch());
                userWithdrawalsRecord.setAddrProvinceCode(userWithdrawalsRecordModel.getAddrProvinceCode());
                userWithdrawalsRecord.setAddrProvinceName(provinceCode.getAreaName());
                userWithdrawalsRecord.setAddrProvinceCode(userWithdrawalsRecordModel.getAddrProvinceCode());
                userWithdrawalsRecord.setAddrCityCode(userWithdrawalsRecordModel.getAddrCityCode());
                userWithdrawalsRecord.setAddrCityName(cityCode.getAreaName());
                userWithdrawalsRecord.setBranch(userWithdrawalsRecordModel.getBranch());
                userWithdrawalsRecord.setCardNo(userWithdrawalsRecordModel.getCardNo());
                userWithdrawalsRecord.setCreateOn(new Date());
                userWithdrawalsRecord.setStatus(0);
                userWithdrawalsRecord.setAccountBalance(balance);
                int returnResult = userWithdrawalsRecordService.addWithdrawalsRecord(userWithdrawalsRecord);
                /**提现记录详情*/
                userAccountDetails.setWalletId(UUID.randomUUID().toString());
                userAccountDetails.setUserWithdrawalsId(userWithdrawalsRecord.getId());
                userAccountDetails.setUserId(user.getUserId());
                userAccountDetails.setUserName(user.getUserName());
                userAccountDetails.setExpenditure(cashWithdrawalAmountBigDecimal);
                userAccountDetails.setType(2);
                userAccountDetails.setCreateOn(new Date());
                userAccountDetails.setStatus(0);
                userAccountDetails.setBalance(balance);
                int withdrawals = userAccountDetailsService.addUserAccountDetails(userAccountDetails);
                //提现后更新user里面的金额
                userService.updateAccountBalance(user.getUserId() ,balance);
                if (returnResult>0&& withdrawals>0){
                    model.addAttribute("result",1);
            }
        }
        return "user_withdraw";
    }

}
